/**
 * @Author: zusw
 * @description: 角色列表命名空间。fetchDetail 调取详情
 * @param {type} 
 */
import {
  page,
  remove,
  add,
  edit,
  getResources,
  getAllResource,
  getUserResource,
  bindResource,
  getTree,
  batchesRemove,
  fetchDetail,
  getResourceData
} from '../services/role';

export default {
  namespace: 'roleBack',
  state: {
    resourceData: [],
    data: {
      searchVals: {},
      records: [],
      pagination: {
        current: 1,
        pageSize: 10
      },
    },
    loading: false,
    resources: [],
    roleDetail: {
      role: {},
      checked: [],
      halfChecked: []
    },
  },

  effects: {
    // 获取角色列表
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(page, payload);
      yield put({
        type: 'getList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    // 获取详情
    *fetchDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(fetchDetail, payload);
      yield put({
        type: 'saveRoleDDetail',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response)
    },
    // 新增时清楚store缓存的角色数据
    *clearRoleDetail({ payload }, { call, put }) {
      yield put({
        type: 'changeAddLoading',
      })
    },
    // 新增角色
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const res = yield call(add, payload);
      const response = yield call(page, payload);
      yield put({
        type: 'getList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(res);
    },
    // 删除
    *remove({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const res = yield call(remove, payload);
      const response = yield call(page, payload);
      yield put({
        type: 'getList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(res);
    },
    *batchesRemove({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield call(batchesRemove, payload);
      const response = yield call(page, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback();
    },
    // 更新
    *update({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(edit, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *getTree({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getTree, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    // 获取详情组织树
    *getResourceData({ payload, callback }, { call, put}) {
      const res = yield call(getResourceData)
      if(res.statusCode === "0"){
        yield put({
          type: 'saveResource',
          payload: { resourceData: res.data.children },
        });
      }
    },
    *getResourcesIds({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getResources, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *getAllResource({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getAllResource, payload);
      yield put({
        type: 'saveAllResource',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *getUserResource({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getUserResource, payload);
      yield put({
        type: 'saveAllResource',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *bindResource({ payload, callback }, { call, put }) {
      const response = yield call(bindResource, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    // 新增保存
    save(state, action) {
      return {
        ...state,
        // data: action.payload && action.payload.data,
      };
    },
    saveResource(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    // 获取列表
    getList(state, action) {
      const pageData = action.payload.data
      state.data.records = pageData.records
      state.data.pagination = {
        current: pageData.current,
        pageSize: pageData.size,
        total: pageData.total
      }
      return {
        ...state
      }
    },
    // 角色详情
    saveRoleDDetail(state, action) {
      return {
        ...state,
        roleDetail: action.payload.data
      }
      // }
    },
    // 新增时清除store中的详情数据
    changeAddLoading(state) {
      return {
        ...state,
        roleDetail: { role: {}, checked: [], halfChecked: [] }
      }
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    saveProject(state, action) {
      return {
        ...state,
        projectDate: action.payload,
      };
    },
    saveAllResource(state, action) {
      return {
        ...state,
        resources: action.payload.children,
      };
    },
    registerHandle(state, { payload }) {
      return {
        ...state,
        list: payload,
      };
    },
  },
};
